var BsaAssistant = Class.create(AbstractModule, {

setupWidgets: function() {

		//About
		this.controller.setupWidget(Mojo.Menu.appMenu, {
			omitDefaultItems: true
		}, {
			visible: true,
			items: [
      		Mojo.Menu.editItem,
			{ label: "Disclaimer", command: "do-disclaimer" },					
				{ label: "License", command: "do-license" },
				{ label: "Help", command: "do-help" }
			]
		});

this.help = true;

	this.toggleunit =
	{
		modelProperty: 'togglestate',
		trueLabel: 'SI',
	falseLabel: 'Imp'
	};
	this.togglemodel = {
		togglestate: true
	};
	this.controller.setupWidget("unitField", this.toggleunit, this.togglemodel);
	this.controller.listen("unitField", Mojo.Event.propertyChanged, this.unitChanged.bindAsEventListener(this));
	
	
		this.controller.setupWidget("sex", {
			label: "Sex",
			choices: [
				{ label: "Male", value: 1},
				{ label: "Female", value: 0}
			]
		}, this.sexModel = {
			value: 1
		});


	this.controller.setupWidget("heightField", {
		charsAllow: isValidFPNumber,
		modifierState: Mojo.Widget.numLock,
	//	focusMode:		Mojo.Widget.focusSelectMode
	}, 
	{
	value: height
	});
	this.controller.setupWidget("weightField", {
		charsAllow: isValidFPNumber,
		modifierState: Mojo.Widget.numLock,
	//	focusMode:		Mojo.Widget.focusSelectMode

	}, 
	{
	value: weight
	});
	
},

unitChanged: function() {
//change unit display in bmi scene
var unitchoice = this.togglemodel.togglestate;

if (!unitchoice){
$("heightUnit").update("<span class=\"si\">(in)</span>");
$("weightUnit").update("<span class=\"si\">(lb)</span>");
}

if (unitchoice){
$("heightUnit").update("<span class=\"si\">(cm)</span>");
$("weightUnit").update("<span class=\"si\">(kg)</span>");
}
},


	calculate: function() {
	var result = bsacalc(this.togglemodel.togglestate,this.sexModel.value, $("heightField").mojo.getValue(), $("weightField").mojo.getValue());
	height=result.height;
	weight=result.weight;
	
	var lbsibw=result.ibw*2.2046;
	lbsibw=Math.round(lbsibw);
	
	var interp="";
	
	if (result.ibw>0){	
	interp= ("The Ideal Body Weight is " + result.ibw +" kg or " + lbsibw + " lbs");
	}
	
	if (result.ibw==0){	
	interp = ("The Ideal Body Weight calculation is not applicable for height under 5 feet (60 inches)");
	}
	
var lbslbw=result.lbw*2.2046;
lbslbw=Math.round(lbslbw);
	
var interp2="normal";
if (result.bmi>29.99) {
interp2="obese";
}
	
if (result.bmi<18.5) {
interp2="underweight";
}
	
if ((result.bmi>24.99) && (result.bmi<30)) {
interp2="overweight";
}
	
	if (result.bsa==0){
	return errormsg;
	}
	else {
	return "The BSA is " + result.bsa +" m<sup>2</sup>" +  "<br />" + interp + "<br />The Lean body weight is " + result.lbw +"kg or " + lbslbw +"lbs" + "<br />The BMI is "+ result.bmi + " which is " +interp2;
	}
	}
});





